/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameClass;

import Core.Status;
import Debug.FrameCounter;
import Game.ShootBalloon;
import Thing.Gift;
import Thing.Monster;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/**
 *
 * @author THINH
 */
public class PlayingScreen extends GameCanvas implements Runnable{
    //field
    private ShootBalloon midlet;
    private boolean isPlay = true;
    private int width;
    private int height;
    private int currentX = 0;
    private int currentY = 0;
    Image backGround;
    Image item;
    
    private Component component;
    
    public PlayingScreen(ShootBalloon midlet){
        super(true);
        setFullScreenMode(true);
        //code here
        this.midlet = midlet;
        width = getWidth();
        height = getHeight();
        component = new Component(width, height);
        try{
            backGround = Image.createImage("/resource/top_background.png");
            //item = Image.createImage("/resource/item/Science_icon.jpg");
            pauseImage = Image.createImage("/resource/screen/PauseScreen.png");
        }
        catch(IOException ioe){
            
        }
    }
    public void startGame(){
        Thread t = new Thread(this);
        t.start();
        if(this.midlet.getSound()){
            Thread music = new Thread(component.sound);
            music.start();
        }
    }
    public void pointerPressed(int x,int y){
        if(pause){
            if(x<getWidth() && x>getWidth()-100
                && y<getHeight() && y>getHeight()-100){
                pause = false;
                this.midlet.setPause(pause);
            }
            
        }
        else{
            if(x<getWidth() && x>getWidth()-150
                && y>0 && y<100){
                pause = true;
                this.midlet.setPause(pause);
                midlet.setCurrent(midlet.getMainMenuScreen());
            }
        }
    }
    
    public void keyPressed(int keyCode)
    {
        component.active(this.getKeyName(keyCode));
    }
    
    private void update(int keyStates){
        
        
        if(pause){
            
            //xet toa do
             
        }
        else{
            //xet toa do 
            component.update(keyStates);
        
            if(component.gameover){
                midlet.setCurrent(midlet.getLoseScreen());
                component.gameover = false;
                component.player.setPos(0,0);
                currentX = currentY = 0;
                component.player.currenthealth = 100;
                Monster mon = (Monster)component.monsters.elementAt(0);
                mon.health = mon.MAX_HEAL;
                mon.setPos(width/2,height/2);
                //component.monsters.removeAllElements();
                //component.monsters.addElement(mon);
                component.player.getGifts().removeAllElements();
                component.MonsterKill = 0;
                pause = true;
            } 
            else {

            }
            
            if(component.gameWin){
                midlet.setCurrent(midlet.getWinScreen());
                component.gameWin = false;
                component.player.setPos(0,0);
                currentX = currentY = 0;
                component.player.currenthealth = 100;
                Monster mon = (Monster)component.monsters.elementAt(0);
                mon.health = mon.MAX_HEAL;
                mon.setPos(width/2,height/2);
                //component.monsters.removeAllElements();
                //component.monsters.addElement(mon);
                component.player.getGifts().removeAllElements();
                component.MonsterKill = 0;
                pause = true;
            }

            updatePlayerView(component);
        }
      
        
    }
    
    private long velSum = 0;
    private int count =0;
    public boolean pause = false;
    private Image pauseImage = null;
  
    private void draw(Graphics g){
        
        if(!pause){
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, width, height);
            //code here

            component.paint(g, 0, 0);        
            //debug here
            long fps = FrameCounter.get_framerate();
            g.setColor(255, 255, 255);
            g.drawString(String.valueOf(fps), width - 50, 50, 0);       

            if(component.player.status.isStand())
            {
               velSum = 0; 
               count = 0;
            }
            else
            {
                velSum+= component.player.getVelocity().x;
                count++;
            }
            if(count!=0)
             g.drawString("ave Vel: "+String.valueOf(velSum/count), width - 100, 80, 0);
            Monster mon = (Monster)component.monsters.elementAt(0);
            g.drawString("mon Health: "+String.valueOf(mon.health), width - 80, 60, 0);
            g.drawString("Health : "+String.valueOf(component.player.currenthealth), width - 80, 100, 0);
            g.drawString("Vel y: "+String.valueOf(mon.getVelocity().y), width - 80, 120, 0);
            g.drawString("x: "+String.valueOf(mon.getX()), width - 80, 140, 0);
            g.drawString("y: "+String.valueOf(mon.getY()), width - 80, 160, 0);
            g.drawString("frame: "+String.valueOf(mon.getSprite().getFrame()), width - 80, 180, 0);
            g.drawString("range: "+String.valueOf(mon.range), width - 80, 200, 0);
            //end debug

            g.drawImage(backGround, 0, 0, 0);
            Vector gifts = component.player.getGifts();
            if (gifts != null) {
                for (int i = 0; i < gifts.size(); i++) {
                    Gift f = (Gift)gifts.elementAt(i);
                    try {
                        if (f.getPath() != null) {
                            item = Image.createImage(f.getPath());
                        }
                    } catch (Exception e) {
                    }
                    if (item != null) {
                        g.drawImage(item, 40 * i, 0, 0);
                    }
                    //item = null;
                }
            }
        }
        else{
            //pausing game
            g.drawImage(pauseImage, 0, 0, 0);
        }
        flushGraphics();
    }
    public void run(){
        Graphics g = getGraphics();
        while(isPlay){
            FrameCounter.frame_shown();
            long startTime = System.currentTimeMillis();
            int keyStates = getKeyStates();
            //update
            update(keyStates);
            //draw
            draw(g);
           
            //sleep
            try{
                long endTime = System.currentTimeMillis();
                int delta = (int)(endTime - startTime);
                int delay = 40 - delta;
                if(delay < 0){
                    delay = 40;
                }
                Thread.sleep(delay);
            }
            catch(Exception ex){
                
            }
           
        }
    }
    private int distanceX =0;
    private int distanceY = 0;
    /*
    void updateBulletView(LayerManager lm){
        int speed = 0;
        
        speed = (int)component.player.flying.getVelocity().x;
        int x = component.player.flying.getX();
        int y = component.player.flying.getY();
        int midX = currentX + width/2-50;
        int midY = currentY + height/2;
        int dX=0 ,dY=0;
        if(x>midX){
            dX=speed;
        }
        else if(x<midX){
        }
        currentX = currentX + dX;
        distanceX +=dX;
        boolean screenMoving = setViewPoint(lm, currentX, currentY, width, height);
        if(!screenMoving){
            currentX= currentX -dX;
            distanceX -=dX;
        }
        
        speed = (int)component.player.flying.getVelocity().y;
        if(y<midY-20){
                dY=-1*speed;
        }
        else if(y>midY+80){
                dY=speed;
        }        
        currentY = currentY + dY;
        distanceY = distanceY + dY;
        boolean screenMoving1 = setViewPoint(lm, currentX, currentY, width, height);
        if(!screenMoving1){
            currentY= currentY -dY;
            distanceY = distanceY - dY;
        }
        
    }
    */
    void updatePlayerView(LayerManager lm){
        
        Status way = component.player.status;
        int speed = 0;
        if (!component.player.status.isJump()) {
            speed = (int) component.player.speed * 9 / 10;
        } else {
            speed = (int) component.player.getVelocity().x;
        }
        int x = component.player.getX();
        int y = component.player.getY();
        int midX = currentX + width / 2 - 50;
        int midY = currentY + height / 2;
        int dX = 0, dY = 0;
        if (x > midX && way.isRight()) {
            dX = speed;
        } else if (x < midX && way.isLeft() || x < currentX) {
            if (!component.player.status.isJump()) {
                dX = -1 * speed;
            } else {
                dX = speed;
            }
        }
        currentX = currentX + dX;
        boolean screenMoving = setViewPoint(lm, currentX, currentY, width, height);
        if (!screenMoving) {
            currentX = currentX - dX;
        }

        speed = (int) component.player.speed;
        if (y < midY - 20) {
            if (!component.player.status.isJump()) {
                dY = -1 * speed;
            } else {
                dY = -1 * speed;
            }
        } else if (y > midY + 80) {
            if (!component.player.status.isJump()) {
                dY = (int) component.player.getVelocity().y;
            } else {
                dY = (int) component.player.getVelocity().y;
            }
        }
        currentY = currentY + dY;
        boolean screenMoving1 = setViewPoint(lm, currentX, currentY, width, height);
        if (!screenMoving1) {
            currentY = currentY - dY;
        }

    }
    
    boolean setViewPoint(LayerManager lm,int x,int y, int width, int height){
        boolean ret = false;
        if(x>=0 && x<= component.mapWidth-width
                && y>= 0 && y<= component.mapHeight-height){
            lm.setViewWindow(x, y, width, height);
            ret = true;
        }
        return ret;
    }
    
}
